package com.yayo.mangement.facade.impl;

import com.yayo.mangement.converter.SysMenuConverter;
import com.yayo.mangement.dto.SysMenuDTO;
import com.yayo.mangement.facade.SysMenuFacade;
import com.yayo.mangement.model.SysMenuDO;
import com.yayo.mangement.paging.Paging;
import com.yayo.mangement.response.Response;
import com.yayo.mangement.service.SysMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 
 * @author: Yayo
 * @date: 2020-07-15 13:23:49
 */
@Service
public class SysMenuFacadeImpl implements SysMenuFacade{

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysMenuConverter sysMenuConverter;

    @Override
    public Response<List<SysMenuDTO>> list(){

        List<SysMenuDO> sysMenuDOList = sysMenuService.list();

        List<SysMenuDTO> sysMenuDTOList = sysMenuDOList.stream().map(sysMenuConverter::DO2DTO).collect(Collectors.toList());

        List<SysMenuDTO> sysMenuDTOS = this.get(sysMenuDTOList,0L);

        return Response.ok(sysMenuDTOS);
    }


    private List<SysMenuDTO> get(List<SysMenuDTO> sysMenuDTOList, Long pid){
        List<SysMenuDTO> list = new ArrayList<>();

        for(SysMenuDTO sysMenuDTO : sysMenuDTOList){

            if (Objects.equals(sysMenuDTO.getMenuPid(), pid)) {

                list.add(sysMenuDTO);

                List<SysMenuDTO> childList = this.get(sysMenuDTOList, sysMenuDTO.getMenuId());

                if (CollectionUtils.isNotEmpty(childList)) {
                    //排序
                    childList.sort((x,y) -> Long.compare(x.getMenuSort(), y.getMenuSort()));
                    sysMenuDTO.setChildList(childList);
                }

            }
        }
        return list;
    }

    @Override
    public Response<SysMenuDTO> detail(Long id){
        SysMenuDO sysMenuDO = sysMenuService.findById(id);
        return Response.ok(sysMenuConverter.DO2DTO(sysMenuDO));
    }

    @Override
    public Response create(SysMenuDTO sysMenuDTO){
        SysMenuDO sysMenuDO = sysMenuConverter.DTO2DO(sysMenuDTO);
        Boolean isTrue = sysMenuService.create(sysMenuDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("create.data.error");
    }

    @Override
    public Response update(SysMenuDTO sysMenuDTO){
        SysMenuDO sysMenuDO = sysMenuConverter.DTO2DO(sysMenuDTO);
        Boolean isTrue = sysMenuService.update(sysMenuDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("update.date.error");
    }

    @Override
    public Response delete(Long id){
        boolean isTrue = sysMenuService.delete(id);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("delete.date.error");
    }

    @Override
    public Response<Paging<SysMenuDTO>> paging() {
        List<SysMenuDO> sysMenuDOList = sysMenuService.list();
        List<SysMenuDTO> sysMenuDTOList = sysMenuDOList.stream().map(sysMenuConverter::DO2DTO).collect(Collectors.toList());
//        List<SysMenuDTO> sysMenuDTOS = this.get(sysMenuDTOList,0L);
        return Response.ok(new Paging<>(sysMenuDTOList.stream().count(), sysMenuDTOList));
    }
}